/// Setting up the ANES File for presidential coalitions  

use "/Users/joshuazingher/Desktop/Prospectus /anes_cdfdta/anes coalitions .dta"

sort year

rename VCF0704 presvote

rename VCF0703 voted

/// Generating total Male respondants by year

by year: egen maleresp = count(gender) if gender == 1

/// Generating total number of Male voters by year


by year: egen Malevoted = count(voted) if gender==1 & voted==3

/// Generating total number of Latnio non-voters by year

by year: egen MaleNvoted = count(voted) if voted==0 & gender==1 | voted==1 & gender==1 | voted==2 & gender==1

/// Generating total number of non-Male voters by year

by year: egen NMalevoted = count(voted) if gender~=1 & voted==3

/// Generating total number of non-Male non-voters by year

by year: egen NMaleNvoted = count(voted) if gender~=1 & voted==0 | gender~=1 & voted==1 | gender~=1 & voted==2

/// filling in the blanks

by year: egen malevoted = max(Malevoted)
by year: egen maleNvoted = max(MaleNvoted)
by year: egen Nmalevoted = max(NMalevoted)
by year: egen NmaleNvoted = max(NMaleNvoted)




/// Generating % Male TO with Mosteller Correction

gen N = 1
gen a = [malevoted * NmaleNvoted] / [maleNvoted * Nmalevoted]


///Generating filler vars

gen aminone =(a-1)
gen AMaleplusTO = (AMale+aTO)
gen aMaleTO=(a*AMale*aTO)

gen CMaleTO = 1/ (2*(aminone)) * (N+(aminone*AMaleplusTO) - sqrt((N+aminone*AMaleplusTO)*(N+aminone*AMaleplusTO) - (4*(aminone*aMaleTO))))

///Adjusting TO for the formula

replace CMaleTO = CMaleTO/AMale



/// Moving to Partisanship - Generating Male Democratic Vote total by year

by year: egen Maledem = count(gender) if presvote==2 & gender==1

/// Generating Male Non-Democratic Vote by Year

by year: egen MaleNdem = count(gender) if presvote==1 & gender==1

// Generating non-Male Democratic Vote total by year

by year: egen NMaledem = count(gender) if presvote==2 & gender~=1

/// Generating non-Male non-Democratic vote total by year

by year: egen NMaleNdem = count(gender) if presvote==1 & gender~=1

/// filling in the blanks

by year: egen maleDem = max(Maledem)
by year: egen maleNDem = max(MaleNdem)
by year: egen NmaleDem = max(NMaledem)
by year: egen NmaleNDem = max(NMaleNdem)

/// Generation % Male Democrat with Mosteller Correction

gen A = (maleDem*NmaleNDem)/(maleNDem*NmaleDem)
gen Aminone =(A-1)
gen AMaleplusDem = (AMale+ARep)
gen AMaleDem=(A*AMale*ARep)
gen CMaleDem = 1/(2*(Aminone)) * (N+(Aminone*AMaleplusDem) - sqrt((N+Aminone*AMaleplusDem)*(N+Aminone*AMaleplusDem) - (4*(Aminone*AMaleDem))))

replace CMaleDem = CMaleDem/AMale


/// Generating number of ANES respondents by year

by year: egen totalresp = count (VCF0006a)


/// Coalition Contribution

gen malecoalcontrib = (CMaleTO* CMaleDem *AMale)/(aTO*ARep)



